<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\BaseDataSaveRequest;
use App\Models\BaseData;
use App\Http\ErrCode;
use Excel;

class BaseDataController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {

        return $this->index_model_list(BaseData::class,['分类','内容'],$request,function($builder) use($request){

            $keyword = $request->get('keyword','');
            $cate_id = $request->get('cate_id',0);

            //自定义查询条件
            if(!empty($keyword)){
                $builder->where('value','like','%'.$keyword.'%');
            }
            if($cate_id > 0){
                $builder->where(['cate_id'=>$cate_id]);
            }
        },function($model) {
            return [
               $model->getTypeText(),
               $model->value
           ];
        });
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(BaseDataSaveRequest $request)
    {
        $data = array_null_to_empty($request->only(['cate_id','value']));

        //检测是否已经存在
        if($this->modelExist(BaseData::class,$data)){
            return json_error(ErrCode::RESOURCE_EXIST,'数据已经存在');
        }

        $model = new BaseData();
        $model->fill($data);
        $model->save();
        return response()->json(json_success(['id'=>$model->id]));
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show(BaseData $model)
    {
        // $model = BaseData::findOrFail($id);
        return response()->json(json_success($model));
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(BaseDataSaveRequest $request, BaseData $model)
    {
        $data = array_filter($request->only(['cate_id','value']));

        //检测是否已经存在
        if($this->modelExist(BaseData::class,$data,$model->id)){
            return json_error(ErrCode::RESOURCE_EXIST,'数据已经存在');
        }

        $model->update($data);
        return response()->json(json_success());
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy(BaseData $model)
    {
        // $model = BaseData::findOrFail($id);
        $model->delete();
        return response()->json(json_success());
    }
}
